<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Hello World Component Controller
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class calendarController extends JController
{
    /**
     * Method to display the view
     *
     * @access    public
     */
    function display($date_obj = NULL)
    {
		global $options,$mainframe;
		$document = &JFactory::getDocument();
	//	var_dump('controller');
		$config = &JFactory::getConfig();
		
		//TODO: update for other api
		$api = $config->getValue('config.bookstore_api', null);
		if($api){
			$this->display_api($date_obj);
			return;
		}

	/*	
		$model =& $this->getModel('calendar');
		//var_dump($model);
		$model->_setBuild('Calendar');
		//var_dump($model->_buildtype);
		//var_dump($model->cal_query);
		$items =  $model->getData();
		//var_dump($items);
		$daysInMonth =& $model->diary2($items);
		//$html = $this->table($daysInMonth);
		//var_dump($daysInMonth);
		$view->assignRef('daysInMonth', $daysInMonth);
		$view->assignRef('items', $items);
		$view->assignRef('html', $html);
		var_dump($api_on);
		$view->display();
	*/			
		/*
			$vType		= $document->getType();
			$vName    = JRequest::getCmd( 'view', $this->_name );
			$vLayout    = JRequest::getCmd( 'layout', 'default' );
			$view = & $this->getView( $vName, $vType, '', array( 'base_path'=>$this->_basePath));
			$model =& $this->getModel($vName);
			$items =  $model->getData($date_obj);
			$cal_obj =& $model->diary2($items, $date_obj);
			$cal_obj->uri->setFragments('year','month','day');
			$view->assignRef('cal_uri', $cal_obj->uri);
			$view->assignRef('cal_text', $cal_obj->textual);
			$view->assignRef('daysInMonth', $cal_obj->daysInMonth);
			$view->assignRef('items', $items);
		
			$view->display_api();
		*/
       parent::display();
    }

	function display_api($date_obj = NULL)
    {
		global $options,$mainframe;
		$api =& new bookstore_api();
		//var_dump($api);
		//$config = &JFactory::getConfig();
		//$rsg2_api = $config->getValue('config.config.plg_rsg2_api', null);
		//if($rsg2_api){	
		//	$rsg2_api->instance();
		//}
		//var_dump('api aware controller');
		
		//if($api && $documnet->getLayout() == 'detail') {
		//var_dump('display_api');
			$document = &JFactory::getDocument();
			$vType		= $document->getType();
			$vName    = JRequest::getCmd( 'view', $this->_name );
			//var_dump($vName);
			$vLayout    = JRequest::getCmd( 'layout', 'default' );
			$view = & $this->getView( $vName, $vType, '', array( 'base_path'=>$this->_basePath));
			
	//		$vName		= $this->_name()//'calendar'; //TODO: get from jrequest
			// Get/Create the view
		//	$view = &$this->getView( $vName, $vType);
			$model =& $this->getModel($vName);
		//	var_dump('model made');
		/*	$task = JRequest::getVar('task');
			switch($task){
				'view':
					$model->_setBuild();
					break;
				'calendar':
				default:
					$model->_setBuild('Calendar');			
			}
			*/
			//var_dump($model->_buildtype);
			//var_dump($model->cal_query);
			$items =  $model->getData($date_obj);
		//	var_dump('items sourced');
			$cal_obj =& $model->diary2($items, $date_obj);
		//	var_dump('daysInMonth made');
			$cal_obj->uri->setFragments('year','month','day');
			$view->assignRef('cal_uri', $cal_obj->uri);
			$view->assignRef('cal_text', $cal_obj->textual);
			$view->assignRef('daysInMonth', $cal_obj->daysInMonth);
			$view->assignRef('items', $items);
		//	$view->assignRef('html', $html);
			$view->display_api();
		//	*/
	//	parent::display();
    }//end display_api()
	
	/**
	*	view single event
	*/
	function view(){
		global $options,$mainframe;
		$api =& new bookstore_api();
		$config = &JFactory::getConfig();
		
		$document = &JFactory::getDocument();
		$vType		= $document->getType();
		$vName    = JRequest::getCmd( 'view', $this->_name );
		
		$vLayout    = JRequest::getCmd( 'layout', 'default' );
		$view = & $this->getView( $vName, $vType, '', array( 'base_path'=>$this->_basePath));
		
		$model =& $this->getModel($vName);
		$item =  $model->getData();
	
		$params = new JParameter( $item[0]->params );
		$assoc_isbn = trim($params->get('assoc_isbn'));
	//	var_dump($assoc_isbn);
		$local = $api->local_detail($assoc_isbn);
		
		// get galleries
		$rsg2_api = $config->getValue('config.config.plg_rsg2_api', null);
		
		if($rsg2_api){
			$file = JPATH_SITE.DS
			.'components/com_calendar/helpers/api.php';
			require_once($file);
			$assoc_gallery_id = trim($params->get('assoc_gallery'));
			if(!empty($assoc_gallery_id)){
		
				
				$switch=1; //parse incoming; assumed to be comma delimited str
				$type = 'array'; //return type
				$array = calendar_rsg2_api::calendarGalleriesStore($assoc_gallery_id, $switch, $type); //just an array of galleries=>templates based on params
				foreach($array as $k=>$v){
					
					if(empty($v)){
						$assoc_gallery_tmpl = trim($params->get('assoc_gallery_tmpl', NULL));
					}else{
						$assoc_gallery_tmpl = $v;
					}
					if(empty($assoc_gallery_tmpl)){
						$newInstance = array(
							'gid' => $k 
						);
					}else{
						$newInstance = array(
							'rsgTemplate' => $assoc_gallery_tmpl,
							'gid' => $k 
						);
					}
					
				 //verify gallery exists or rsgallery will complain
					$db =& JFactory::getDBO();
					$query = 'select id from #__rsgallery2_galleries where id= '.$db->Quote($k);
					$db->setQuery( $query );
					if (!$db->query()) {
						return JError::raiseWarning( 500, $db->getErrorMsg());
					}//end if query
					
					if($db->loadResult()){
						ob_start();
							calendar_rsg2_api::instance($newInstance);
							$content_output[$k] = ob_get_contents();
						ob_end_clean();	
					}//end if id exists
				}//end foreach gallery id
				$view->assignRef('gallery', $content_output);
		/**/
			}//end if there are any galleries
		}//end if rsg2 api is active/published
	
		//var_dump($local->query);
	//	$external = $api->external_detail($assoc_isbn);
		
		$view->assignRef('params',$params);
		$view->assignRef('item', $item[0]);
		$view->assignRef('local', $local);
	//	$view->assignRef('external', $external);
		$view->display_api();
	}
	
	/**
	*	sets event parameters(which month,day, etc);switch between tabular/list?
	*/
	function show(){
		global $options, $mainframe;
		
		//get style, ie tabluar,list
		//get parameters, ie month,day
		$this->display();
	}

}
?>